package com.wisemoresystem.seal.action.seal.type.config;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.wisemoresystem.seal.base.action.ProjBaseAction;
import com.wisemoresystem.seal.base.tip.Tip;
import com.wisemoresystem.seal.model.t_seal_type_config;
import com.wisemoresystem.seal.service.BankService;
import com.wisemoresystem.seal.service.SealTypeConfigService;
import com.wisemoresystem.seal.web.WebTools;

/**
 * 印章类型配置
 * 
 * @author Administrator
 *
 */
@Controller("sealConfig")
@Scope("prototype")
@RequestMapping("/ix/seal/config")
public class SealTypeConfigAction extends ProjBaseAction {

	@Autowired
	private BankService bankService;
	@Autowired
	private SealTypeConfigService sealTypeConfigService;

	/**
	 * 印章类型配置列表
	 * 
	 * @return
	 */
	@RequestMapping("/list")
	public ModelAndView list() {

		List<Map<String, Object>> list = sealTypeConfigService
				.selectSealTypeConfig();

		ModelAndView mv = new ModelAndView("/jsp/seal/config/list");
		mv.addObject("list", list);
		return mv;
	}

	/**
	 * 添加页面
	 * 
	 * @return
	 */
	@RequestMapping("/add_form")
	public ModelAndView add_form() {
		List<Map<String, Object>> bank = bankService.selectBankLevel();
		ModelAndView mv = new ModelAndView("/jsp/seal/config/add_form");
		mv.addObject("bank", bank);
		return mv;
	}

	/**
	 * 类型添加
	 * 
	 * @param seal_type_config
	 *            类型id
	 * @return
	 */
	@RequestMapping("/add")
	public ModelAndView add(t_seal_type_config seal_type_config) {
		seal_type_config.setUser_id(WebTools.getCurrentUserId());
		oc.saveObj(seal_type_config);
		super.setTipMsg("修改成功", Tip.Type.success);
		logT.logH("添加印章类型", t_seal_type_config.class, seal_type_config.get_id());
		ModelAndView mv = new ModelAndView("redirect:list.htm");
		return mv;
	}

	/**
	 * 要修改的种类信息
	 * 
	 * @param seal_type_config_id
	 *            类型id
	 * @return
	 */
	@RequestMapping("/edit_form")
	public ModelAndView edit_form(String seal_type_config_id) {

		List<Map<String, Object>> bank = bankService.selectBankLevel();

		t_seal_type_config seal_type_config = (t_seal_type_config) oc
				.findObjById(seal_type_config_id, t_seal_type_config.class);

		ModelAndView mv = new ModelAndView("/jsp/seal/config/edit_form");
		mv.addObject("seal_type_config", seal_type_config);
		mv.addObject("bank", bank);
		return mv;
	}

	/**
	 * 种类修改
	 * 
	 * @param seal_type_config
	 *            类型信息
	 * @param seal_type_config_id
	 *            类型id
	 * @return
	 */
	@RequestMapping("/edit")
	public ModelAndView edit(t_seal_type_config seal_type_config,
			String seal_type_config_id) {

		t_seal_type_config seal_type_configDb = (t_seal_type_config) oc
				.findObjById(seal_type_config_id, t_seal_type_config.class);
		seal_type_configDb.setCode(seal_type_config.getCode());
		seal_type_configDb.setName(seal_type_config.getName());
		seal_type_configDb.setNote(seal_type_config.getNote());
		seal_type_configDb.setUser_id(seal_type_config.getUser_id());
		seal_type_configDb.setUser_way(seal_type_config.getUser_way());
		seal_type_configDb.setVerify_destory(seal_type_config
				.getVerify_destory());
		seal_type_configDb.setVerify_new(seal_type_config.getVerify_new());
		seal_type_configDb.setVerify_stop(seal_type_config.getVerify_stop());
		seal_type_configDb.setUser_id(WebTools.getCurrentUserId());

		oc.updateObj(seal_type_configDb);

		logT.logH("修改印章类型", t_seal_type_config.class, seal_type_config.get_id());

		super.setTipMsg("修改成功", Tip.Type.success);

		ModelAndView mv = new ModelAndView("redirect:list.htm");
		return mv;
	}

	/**
	 * 种类删除
	 * 
	 * @param seal_type_config_id
	 *            类型id
	 * @return
	 */
	@RequestMapping("/del")
	public ModelAndView del(String seal_type_config_id) {
		boolean flag = sealTypeConfigService
				.checkTypeConfig(seal_type_config_id);
		if (flag) {
			oc.delObjById(seal_type_config_id, t_seal_type_config.class);
			logT.logH("删除印章类型", t_seal_type_config.class, seal_type_config_id);
			super.setTipMsg("删除成功", Tip.Type.success);
		} else {
			super.setTipMsg("删除失败,该类型有印章在用，或者该类型在申请中!", Tip.Type.error);
		}

		ModelAndView mv = new ModelAndView("redirect:list.htm");
		return mv;
	}
}
